﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 按键时序
{
    public partial class Form1 : Form
    {

        SocketClient SC;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string host = "";
            int port = 80;

            Console.WriteLine(IP_textBox.Text);
            
            string[] strArry = IP_textBox.Text.Split(':');

            try
            {
                host = strArry[0];
                port = int.Parse(strArry[1]);
            }
            catch(FormatException)
            {
                MessageBox.Show("IP无效,重新输入");
                IP_textBox.Text = "192.168.0.106:80";
            }

            if (!checkIP(host))
            {
                return;
            }

            SC = new SocketClient(host, port);
        }

        bool checkIP(string address)
        {
            IPAddress ipAddress;
            if (IPAddress.TryParse(address, out ipAddress))
            {
                //MessageBox.Show(string.Format("有效{0}", ipAddress));
                Console.WriteLine("IP格式正确");
                return true;
            }
            else
            {
                MessageBox.Show("IP无效,重新输入");
                return false;
            }


    }

        private void IP_textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            Console.WriteLine(e.KeyChar);
            if (char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == ':' || e.KeyChar == (char)Keys.Back)
            {
                return;
            }      
            e.Handled = true;
        }
    }
}
